#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import common

class SearchException(common.CommandException):
    def __init__(self, message):
        self.__message = message

    def __str__(self):
        return repr(self.__message)

class SearchCommand(common.Command):
    def __init__(self, docker, cache):
        self.__docker = docker        
        self.__cache = cache

    def execute(self, **kwargs):
        temp = []

        if "full_name" in kwargs:
            full_name = kwargs["full_name"]
            item = self.__cache.get_item(full_name)
            temp.append(set([item]))

        if "name" in kwargs:            
            name = kwargs["name"]
            list = self.__cache.list()
            temp.append(set(filter(lambda x: x.startswith(name), list)))

        if "get_all" in kwargs:
            if kwargs["get_all"]:
                temp.append(set(self.__cache.list()))

        result = list(reduce(lambda x,y: x & y, temp))

        return result